package com.offcn.controller;

import com.offcn.common.JsonController;
import com.offcn.pojo.Department;
import com.offcn.pojo.Member;
import com.offcn.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("department")
public class DepartmentController  extends JsonController {

    @Autowired
    DepartmentService departmentService;


    /*
       需求：展示部门列表
      URL；http://localhost:8080/eastCMS/department/getAll
       逻辑：
           1：接收请求
           2：查询部门（权限）
           3：将数据放入json
     */
    @RequestMapping("getAll")
    public void getAll(HttpServletRequest request, HttpServletResponse response){

        Member memberSession = (Member) request.getSession().getAttribute("member");

        List<Department> departmentList =   departmentService.selectDepartmentByMemberSession(memberSession);

        JsonUtilObjecToJson(response,"200","success",departmentList);




    }
    /*  需求：保存新建的部门
        URL:http://localhost/eastCMS/department/save
        逻辑：1.接受请求
              2.查询登录人员权限 （经理）
              3.完毕之后返回个成功与否
     */

    @RequestMapping("save")
    public  void  save(HttpServletRequest request,HttpServletResponse response){
        Member memberSession = (Member) request.getSession().getAttribute("member");
        String dname = request.getParameter("dname");
        boolean flag=departmentService.save(memberSession,dname);
            if (flag)
                JsonUtilObjecToJson(response, "200", "恭喜您添加部门成功、思密达", null);
            else
                JsonUtilObjecToJson(response, "400", "恭喜您添加部门失败、思密达", null);


        }

    /*  需求：删除已有部门
        URL:http://localhost/eastCMS/department/delete/5496955
        逻辑：1.接受请求
              2.查询登录人员权限 （经理）
              3.完毕之后返回个成功与否
     */
        @RequestMapping("delete/{did}")
        public  void delete(@PathVariable String did, HttpServletRequest request, HttpServletResponse response) {
            Member memberSession = (Member) request.getSession().getAttribute("member");

            boolean flag = departmentService.deleteByDepartment(memberSession,Integer.valueOf(did));
            if (flag)
                JsonUtilObjecToJson(response, "200", "恭喜您添加部门成功、思密达", null);
            else
                JsonUtilObjecToJson(response, "400", "恭喜您添加部门失败、思密达", null);


        }

        /*  需求：修改部门
        URL:http://localhost/eastCMS/department/update
        逻辑：1.接受请求
              2.查询登录人员权限 （经理）
              3.完毕之后返回个成功与否
     */

    @RequestMapping("update")
    public  void  update(HttpServletRequest request,HttpServletResponse response,Department department){
        Member memberSession = (Member) request.getSession().getAttribute("member");
        boolean flag=departmentService.update(memberSession,department);
        if (flag)
            JsonUtilObjecToJson(response, "200", "恭喜您修改成功、思密达", null);
        else
            JsonUtilObjecToJson(response, "400", "恭喜您修改失败、思密达", null);


    }





}
